<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class BaseModel
{
	//end for

    public $_TABLE_NAME;
    public $_MODEL;
    public function __construct()
    {
        \DB::setFetchMode(\PDO::FETCH_ASSOC);
    }

    function getModelKeys()
    {
        return $this->_MODEL;
    }

    function filterFields($entity)
    {
        $after = array();
		if($this->_MODEL){
           foreach($this->_MODEL as $value)
           {
               $after[$value] = isset($entity[$value])?$entity[$value]:'';
           }
		}
        return array_filter($after);
    }
    function getAll($entity_where=array(), $orderby=array())
    {
        $query = $this->parseWhere($entity_where, $orderby);
        return $query->get();
    }

	function countWhere($entity_where=array())
	{
		$query = $this->parseWhere($entity_where);
		return $query->count();
	}

    function getAllOr($entity_where=array(), $orderby=array())
    {
        $query = $this->parseOrWhere($entity_where, $orderby);
        return $query->get();
    }

    function getOne($entity_where=array(), $orderby=array())
    {
        $entities = $this->getall($entity_where, $orderby);
        if(count($entities)>0)
        {
            return $entities[0];
        }else{
            return array();
        }
    }
    function insert($entity)
    {
        $entity = $this->filterFields($entity);
        //dd($entity);
        return \DB::table($this->_TABLE_NAME)->insertGetId($entity);
    }

	//todo delete is keep by qloquent
    function deleteWhere($entity)
    {
        $query = $this->parseWhere($entity);
        return $query->delete();
    }


    function on_off($entity, $key)
    {
        $status = $this->parseWhere($entity)->pluck($key);
        $target_status = ($status[0]+1)%2;
        return $this->parseWhere($entity)->update(array($key => $target_status));
    }

	//update
    function updateWhere($entity_where, $entity)
    {
        //$entity = $this->filterFields($entity);
        return $this->parseWhere($entity_where)->update($entity);

    }

    function incer($entity_where, $entity)
    {
        $query = $this->parseWhere($entity_where);
        if(is_array($entity))
        {
            foreach($entity as $key => $value)
            {
                if($value>0)
                {
                    $query = $query->increment($key, $value);
                }else{
                    $query = $query->increment($key);
                }
            }
        }else{
            $query = $query->increment($entity);
        }
        return $query;
    }

    function decre($entity_where, $entity)
    {
        $query = $this->parseWhere($entity_where);
        if(is_array($entity))
        {
            foreach($entity as $key => $value)
            {
                $query = $query->decrement($key, $value);
            }
        }else{
            $query = $query->decrement($entity);
        }
        return $query;
    }

    function parseWhere($where, $orderby=array())
    {
//        if(count($where)<1)
//        {
//            return "Parm Empty";
//        }
        $query = \DB::table($this->_TABLE_NAME);

        foreach ($where as $key => $value)
        {
            $query = $query->where($key, $value);
        }
        foreach ($orderby as $key => $value)
        {
            $query = $query->orderby($key, $value);
        }
        return $query;
    }

    function parseOrWhere($where, $orderby=array())
    {
        $query = \DB::table($this->_TABLE_NAME);
        $i = 0;
        foreach ($where as $key => $value)
        {
            if($i == 0)
            {
                $query = $query->where($key, $value);
            }else{
                $query = $query->orWhere($key, $value);
            }
            $i++;
        }
        foreach ($orderby as $key => $value)
        {
            $query = $query->orderby($key, $value);
        }
        return $query;
    }

}
